package com.jackiechan.day12;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class ListDir {
    public static void main(String[] args) throws IOException {
        File file = new File("E:");
//        OutputStream out = new BufferedOutputStream(
//                new FileOutputStream(
//                        "D:\\contains.txt",true));
        OutputStream out = new FileOutputStream(
                        "E:\\zcontains.txt",true);
        show(file, out,"");
    }
    public static void show(File file, OutputStream out, String t) throws IOException {
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                String str = t +files[i].getName() + " length: " + files[i].length() + "\n";
                writeout(str, out);
            } else if (files[i].isDirectory()) {
                File[] files1 = files[i].listFiles();
                if (files1.length == 0) {
                    String str = t+"Directory name: " + files[i].getName()+"\n";
                    writeout(str, out);
                }else {
                    String str = t+files[i].getName() + " is a Directory :\n";
                    writeout(str, out);
                }
                t += "\t";
                show(files[i], out,t);
            }
        }
    }
    public static void writeout (String str, OutputStream out) throws IOException {
        char[] chars = str.toCharArray();
//        System.out.println(Arrays.toString(chars));
        for (int j=0;j<chars.length;j++) {
            int b = chars[j];
            out.write(b);
        }
    }
}

